var Global_Constant= {
	STATUS :1
}
var MsgList = function () {
    var initPickers = function () {
        // init date pickers
        $('.date-picker').datepicker({
            rtl: Metronic.isRTL(),
            autoclose: true
        });
    }
    
    var checkbox = function(){
    	$("#cbox").change(function() {
    		if(document.getElementById("cbox").checked){
    			document.getElementById("cbox").checked = true;
    			$(".mail-checkbox").parents("span").addClass("checked");
    			$(".mail-checkbox").each(function(obj){
    				this.checked = true;
    			});
    		}else{
    			document.getElementById("cbox").checked = false;
    			$(".mail-checkbox").parents("span").removeClass("checked");
    			$(".mail-checkbox").each(function(obj){
    				this.checked = false;
    			});
    		}
    	});
    } 
    
    var refillgrid = function (obj){ // 重新填充数据
    	
    }
    var handleOrders = function() {

        var grid = new Datatable();
            grid.init({
                src: $("#datatable_msgs"),
                onSuccess: function(grid) {
                   console.log(grid);
                },
                onError: function(grid) {
                    // execute some code on network or other general error
                },
                
                dataTable: {  // here you can define a typical datatable
								// settings from
								// http://datatables.net/usage/options
                    "aLengthMenu": [
                        [20, 50, 100, 150, -1],
                        [20, 50, 100, 150, "All"] // change per page values
													// here
                    ],
                    "aoColumns": [   {"mDataProp":"id","sClass": "center" },
                                     {"mDataProp":"ismark","sClass": "center" },
                                     {"mDataProp":"ismark","sClass": "center"},
                                     {"mDataProp":"nickname","sClass": "center"},
                                     {"mDataProp":"message","sClass": "center"},
                                     {"mDataProp":"createTime","sClass": "center"},
                                     ],
                                     
                     "aoColumnDefs": [
// { "aTargets": [7],
// "bSearchable": false,
// "sType": 'date',
// "fnRender": function ( oObj ) {
//                                    		  
// var mDate = moment(oObj.aData.createTime);
//                                    	 
// return mDate.format("YYYY-MM-DD HH:mm");
// }
// },
								  	{ "aTargets": [0], 
                                	   "bVisible": false
                                     },
// { "aTargets": [6],
// "bSearchable": false,
// "sType": 'date',
// "fnRender": function ( oObj ) {
//                                  		  
// var mDate = moment(oObj.aData.expireTime);
//                                  	 
// return mDate.format("YYYY-MM-DD HH:mm");
// }
// },
//                                   
                                     { "aTargets": [5], 
                                  	   "bSearchable": false, 
                                  	   "sType": 'date',
                                  	   "fnRender": function ( oObj ) {
                                  		  
                                  		   			var mDate1 = moment(oObj.aData.createTime);
                                  	                return  mDate1.format("YYYY-MM-DD HH:mm");
                                  	   	}
                                     },
// { "aTargets": [7],
// "bSearchable": false,
// "sType": 'status',
// "fnRender": function ( oObj ) {
// var mstatus = oObj.aData.status;
// if(mstatus=="display"){
// return "公开";
// }else if(mstatus=="hidden"){
// return "隐藏";
// }else{
// return "废弃";
// }
// }
// },
                                     { "aTargets": [1], 
                                    	   "bSearchable": false, 
                                    	   "mData": "",
                                           "mRender": function (oObj, type, full) {
                                          	 console.log(full);
                                          	 var btn = "<span class=''><input type='checkbox' class='mail-checkbox' value='"+full.id+"'/></span>";
                                          	return  btn;
                                     	}
                                     },
                                     { "aTargets": [2], 
                                  	   "bSearchable": false, 
                                  	   	"sType": "int",
                                         "fnRender": function (oObj) {
                                        	 var btn = "<i class='fa fa-star'></i>";
                                        	 if(oObj.aData.ismark>0){
                                        		 btn = "<i class='fa fa-star inbox-started'></i>";
                                        	 }
                                        	return  btn;
                                   	}
                                   },
// {
// "aTargets": [9],
// "mData": "操作",
// "mRender": function (data, type, full) {
// console.log(full);
//                                    	 
// var btn = "<div class='margin-bottom-5'><a class='btn btn-default btn-sm'
// href='admin/event/toedit/"+full.id+".htm'><i class='fa
// fa-pencil'>编辑</i></a>";
// btn+="</div>";
// // return '<div><a class="btn blue"
// href="admin/event/toedit/'+full.id+'.htm"> 编辑</a></div><div><a class="btn
// blue" href="admin/event/toedit/'+full.id+'.htm">详情</a></div>';
// // return '<a href="javascript:void(0);" class="delete" tag=' + data +
// '>删除</a>';
// return btn;
//                                    	 
//                                    	 
// }
// },
                      ],
                    "iDisplayLength": 20, // default record count per page
                    "bServerSide": false, // server side processing
                    "sAjaxSource" : "admin/msg/msg_list.json?now=" + new Date().getTime(),
                    "fnServerData" : function(sSource, aDataSet, fnCallback) {  
                        $.ajax({  
                            "dataType" : 'json',  
                            "data":{
                            	"status":Global_Constant.STATUS
                            	
                            },
                            "type" : "POST",  
                            "url" : "admin/msg/msg_list.json",  
                            "success" : fnCallback  
                        });  
                    },  
                    "aaSorting": [[ 1, "asc" ]] // set first column as a default
												// sort by asc
                }
            });
        	$('.btn btn-sm yellow filter-submit margin-bottom').click(function () {
        		grid.fnDraw();
        	});
        	
        	$("#read").click(function(){
        		$("#msgtab li").removeClass("active");
        		$("#msgtab li.sent").addClass("active");
        		document.getElementById("cbox").checked = false;
    			$(".mail-checkbox").parents("span").removeClass("checked");
        		Global_Constant.STATUS = 2;
        		grid.getDataTable().fnDraw();
        	});
        	$("#trash").click(function(){
        		$("#msgtab li").removeClass("active");
        		$("#msgtab li.trash").addClass("active");
        		document.getElementById("cbox").checked = false;
    			$(".mail-checkbox").parents("span").removeClass("checked");
        		Global_Constant.STATUS = 3;
        		grid.getDataTable().fnDraw();
        	});
        	$("#inbox").click(function(){
        		$("#msgtab li").removeClass("active");
        		$("#msgtab li.inbox").addClass("active");
        		document.getElementById("cbox").checked = false;
    			$(".mail-checkbox").parents("span").removeClass("checked");
        		Global_Constant.STATUS = 1;
        		grid.getDataTable().fnDraw();
        	});
        	$("#cmark").click(function(){
        		var arry ="";
        		$(".mail-checkbox").each(function(obj){
        			if(this.checked&&this.id !="cbox"){
        				arry= arry +this.value+",";
        			}
        		});
        		arry = arry.substring(0, arry.length-1);
        		test = arry;
        		$.ajax({  
                    "dataType" : 'json',  
                    "data":{
                    	"list":arry,
                    	"ismark":1
                    	
                    },
                    "type" : "POST",  
                    "url" : "admin/msg/updateAll.json",
                    "success" : function(data){
                    	grid.getDataTable().fnDraw();
                    	$("#cbox").parent("span").removeClass("checked");
                    }  
                });  
        		
        	});
        	$("#cunmark").click(function(){
        		var arry ="";
        		$(".mail-checkbox").each(function(obj){
        			if(this.checked&&this.id !="cbox"){
        				arry= arry +this.value+",";
        			}
        		});
        		arry = arry.substring(0, arry.length-1);
        		test = arry;
        		$.ajax({  
                    "dataType" : 'json',  
                    "data":{
                    	"list":arry,
                    	"ismark":0
                    	
                    },
                    "type" : "POST",  
                    "url" : "admin/msg/updateAll.json",
                    "success" : function(data){
                    	grid.getDataTable().fnDraw();
                    	$("#cbox").parent("span").removeClass("checked");
                    }  
                }); 
        	});
        	$("#cdele").click(function(){
        		var arry ="";
        		$(".mail-checkbox").each(function(obj){
        			if(this.checked&&this.id !="cbox"){
        				arry= arry +this.value+",";
        			}
        		});
        		arry = arry.substring(0, arry.length-1);
        		test = arry;
        		$.ajax({  
                    "dataType" : 'json',  
                    "data":{
                    	"list":arry,
                    	"status":3
                    	
                    },
                    "type" : "POST",  
                    "url" : "admin/msg/deleteAll.json",
                    "success" : function(data){
                    	grid.getDataTable().fnDraw();
                    	$("#cbox").parent("span").removeClass("checked");
                    }  
                }); 
        	});
        	
            // handle group actionsubmit button click
            grid.getTableWrapper().on('click', '.table-group-action-submit', function(e){
                e.preventDefault();
                var action = $(".table-group-action-input", grid.getTableWrapper());
                if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
                    grid.addAjaxParam("sAction", "group_action");
                    grid.addAjaxParam("sGroupActionName", action.val());
                    var records = grid.getSelectedRows();
                    for (var i in records) {
                        grid.addAjaxParam(records[i]["name"], records[i]["value"]);    
                    }
                    grid.getDataTable().fnDraw();
                    grid.clearAjaxParams();
                } else if (action.val() == "") {
                    Metronic.alert({type: 'danger', icon: 'warning', message: 'Please select an action', container: grid.getTableWrapper(), place: 'prepend'});
                } else if (grid.getSelectedRowsCount() === 0) {
                    Metronic.alert({type: 'danger', icon: 'warning', message: 'No record selected', container: grid.getTableWrapper(), place: 'prepend'});
                }
            });

    }

    return {

        // main function to initiate the module
        init: function () {
            initPickers();
            checkbox();
            handleOrders();
        }
    };

}();
var test ={};





